const { convertToASSColor } = require("./assStyles");

/**
 * 生成单词ASS字幕文件的样式部分
 * @param {object} wordStyles - 单词样式配置
 * @returns {string} ASS样式定义
 */
function generateWordASSStyles(wordStyles) {
  // 默认样式配置
  const defaults = {
    word: {
      fontSize: 14,
      fontFamily: "Arial",
      color: "#00FF00",
      backgroundColor: "#00000080",
      outlineColor: "#000000",
      bold: 0,
      italic: 0,
      alignment: 8,
    },
    phonetic: {
      fontSize: 10,
      fontFamily: "Arial",
      color: "#FFFF00",
      backgroundColor: "#00000000",
      outlineColor: "#000000",
      bold: 0,
      italic: 1,
      alignment: 8,
    },
    translation: {
      fontSize: 12,
      fontFamily: "Arial",
      color: "#00FFFF",
      backgroundColor: "#00000000",
      outlineColor: "#000000",
      bold: 0,
      italic: 0,
      alignment: 8,
    },
  };

  // 合并配置
  const config = {
    word: { ...defaults.word, ...((wordStyles && wordStyles.word) || {}) },
    phonetic: {
      ...defaults.phonetic,
      ...((wordStyles && wordStyles.phonetic) || {}),
    },
    translation: {
      ...defaults.translation,
      ...((wordStyles && wordStyles.translation) || {}),
    },
  };

  // 生成样式部分
  let styles = `[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
`;
  let MarginL = 10;
  let MarginV = 10;

  if (process.env.wordType === "nomoveeffect") {
    MarginL = 120;
    MarginV = 100;
  }
  // 单词样式
  const wordPrimaryColor = convertToASSColor(config.word.color);
  const wordBackColor = convertToASSColor(config.word.backgroundColor);
  const wordOutlineColor = convertToASSColor(config.word.outlineColor);
  styles += `Style: Word,${config.word.fontFamily},${config.word.fontSize},${wordPrimaryColor},&H000000FF,${wordOutlineColor},${wordBackColor},${config.word.bold},${config.word.italic},0,0,100,100,0,0,1,10,0,${config.word.alignment},${MarginL},10,${MarginV},1
`;

  // 音标样式
  const phoneticPrimaryColor = convertToASSColor(config.phonetic.color);
  const phoneticBackColor = convertToASSColor(config.phonetic.backgroundColor);
  const phoneticOutlineColor = convertToASSColor(config.phonetic.outlineColor);
  styles += `Style: Phonetic,${config.phonetic.fontFamily},${config.phonetic.fontSize},${phoneticPrimaryColor},&H000000FF,${phoneticOutlineColor},${phoneticBackColor},${config.phonetic.bold},${config.phonetic.italic},0,0,100,100,0,0,1,2,0,${config.phonetic.alignment},${MarginL},10,${MarginV},1
`;

  // 翻译样式
  const translationPrimaryColor = convertToASSColor(config.translation.color);
  const translationBackColor = convertToASSColor(config.translation.backgroundColor);
  const translationOutlineColor = convertToASSColor(config.translation.outlineColor);
  styles += `Style: Translation,${config.translation.fontFamily},${config.translation.fontSize},${translationPrimaryColor},&H000000FF,${translationOutlineColor},${translationBackColor},${config.translation.bold},${config.translation.italic},0,0,100,100,0,0,1,2,0,${config.translation.alignment},${MarginL},500,${MarginV},1
`;

  return styles;
}

module.exports = {
  generateWordASSStyles,
};
